//#include "y.tab.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "QueryPlan.h"
//#include "QueryExecutor.h"

extern "C" struct YY_BUFFER_STATE *yy_scan_string(const char*);
extern "C" int yyparse(void);

extern struct FuncOperator *finalFunction; // the aggregate function (NULL if no agg)
extern struct TableList *tables; // the list of tables and aliases in the query
extern struct AndList *boolean; // the predicate in the WHERE clause
extern struct NameList *groupingAtts; // grouping atts (NULL if no grouping)
extern struct NameList *attsToSelect; // the set of attributes in the SELECT (NULL if no such atts)
extern struct AttList *attsToCreate; // set of attributes that will be associated with the new table
extern int distinctAtts; // 1 if there is a DISTINCT in a non-aggregate query
extern int distinctFunc;  // 1 if there is a DISTINCT in an aggregate query

using namespace std;


char *fileName = "Statistics.txt";


void q0 (){

    char *cnf = "SELECT r.r_name FROM region AS r WHERE (r.r_regionkey = 1)";
	yy_scan_string(cnf);
	yyparse();
    QueryPlan *obj = new QueryPlan(1);
    Node *root = obj->GetOptimalPlan();
    if (root == NULL)
        cout << "No Join tree \n";
    else
    {
        obj->inOrderPrint(root);
    }

//	char *cnf = "SELECT r.r_name FROM region AS r WHERE (r.r_regionkey = 1)";
//	yy_scan_string(cnf);
//	yyparse();
//
//	Statistics s;
//    char *relName[] = {"part", "partsupp", "supplier", "nation", "region","customer", "lineitem", "orders"};
//    s.AddRel(relName[0], 200000);
//    s.AddAtt(relName[0], "p_partkey", 200000);
//    s.AddAtt(relName[0], "p_size", 50);
//
//    s.AddRel(relName[1], 800000);
//    s.AddAtt(relName[1], "ps_suppkey", 10000);
//    s.AddAtt(relName[1], "ps_partkey", 200000);
//
//    s.AddRel(relName[2], 10000);
//    s.AddAtt(relName[2], "s_suppkey", 10000);
//    s.AddAtt(relName[2], "s_nationkey", 25);
//    s.AddAtt(relName[2], "s_acctbal", 1000);
//
//    s.AddRel(relName[3], 25);
//    s.AddAtt(relName[3], "n_nationkey", 25);
//    s.AddAtt(relName[3], "n_regionkey", 5);
//
//    s.AddRel(relName[4], 5);
//    s.AddAtt(relName[4], "r_regionkey", 5);
//    s.AddAtt(relName[4], "r_name", 5);
//
//    s.AddRel(relName[5], 150000);
//    s.AddAtt(relName[5], "c_custkey", 150000);
//    s.AddAtt(relName[5], "c_mktsegment", 5);
//    s.AddAtt(relName[5], "c_name", 150000);
//    s.AddAtt(relName[5], "c_nationkey", 10000);
//
//    s.AddRel(relName[6], 6001215);
//    s.AddAtt(relName[6], "l_orderkey", 1500000);
//    s.AddAtt(relName[6], "l_quantity", 15000);
//    s.AddAtt(relName[6], "l_discount", 10000);
//    s.AddAtt(relName[6], "l_returnflag", 10000);
//    s.AddAtt(relName[6], "l_suppkey", 10000);
//    s.AddAtt(relName[6], "l_shipmode", 10000);
//
//    s.AddRel(relName[7], 1500000);
//    s.AddAtt(relName[7], "o_custkey", 150000);
//    s.AddAtt(relName[7], "o_totalprice", 10000);
//    s.AddAtt(relName[7], "o_orderkey", 10000);


    //QueryPlanTree *qpt = new QueryPlanTree(&s);
    //qpt->Create();
}

int main(int argc, char *argv[]) {
	q0();
}
